package com.hyk.commonLib.common.drawable;

import android.graphics.Bitmap;

import androidx.annotation.Nullable;

import com.hyk.commonLib.common.utils.ResUtils;
import com.hyk.commonLib.common.utils.SoftHashMap;

import java.text.MessageFormat;

/**
 * Created by 贺玉琨 on 2020/5/4.
 */
public abstract class BaseResFrameAnimDrawable extends BaseFrameAnimDrawable {
    protected SoftHashMap<String, Bitmap> frameCache = new SoftHashMap<>();
    private final int[] imageIdArray;

    public BaseResFrameAnimDrawable() {
        this.imageIdArray = getImageIdArray();
    }

    @Nullable
    @Override
    protected Bitmap getCurrentBitmap(int currentProgress, int width, int height) {
        int id = imageIdArray[currentProgress];
        String key = MessageFormat.format("{0}_size:{1}_{2}", id, width, height);
        Bitmap currentBitmap = frameCache.get(key);
        if (currentBitmap == null) {
            currentBitmap = ResUtils.getBitmap(id, width, height);
            frameCache.put(key, currentBitmap);
        }
        return currentBitmap;
    }

    @Override
    protected int getFrameCnt() {
        return getImageIdArray().length;
    }

    @Override
    public void clearCache() {
        super.clearCache();
        frameCache.clear();
    }

    protected abstract int[] getImageIdArray();
}
